﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
namespace School.Pages.Bus
{
    public partial class District : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1206) == true)
                {

                    if (!IsPostBack)
                    {
                        ddlBind(ddCamp, School.Service.Bus.GetAllCampuses().ToList());
                        ddlBind(ddCampse, School.Service.Bus.GetAllCampuses().ToList());
                        gvRegionBind();
                    }
                    lbltest.Visible = false;
                    lbltestsearch.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "-1"));
            ddl.DataBind();
        }
        #region Region
        private void gvRegionBind()
        {
            int count;
            IEnumerable<LU_District> query = School.Service.Bus.GetDistrict();
            if (ddCampse.SelectedIndex > 0)
                query = query.Where(s => s.Campus_Id == Convert.ToInt32(ddCampse.SelectedValue));
            if (txtcodeSe.Text.Length > 0)
                query = query.Where(s => s.Dis_Code.Contains(txtcodeSe.Text));
            if (txtNameSe.Text.Length > 0)
                query = query.Where(s => s.Name.Contains(txtNameSe.Text));
            var gv = (from q in query
                      join camp in db.ORG_Campus on q.Campus_Id equals camp.Id
                      select new
                      {
                          camp.Name,
                          DistName = q.Name,
                          q.Dis_Code,
                          q.Id
                      }).ToList();
            gvRegion.DataSource = gv;
            gvRegion.DataBind();
            count = gvRegion.Rows.Count;
            lblRecordNo.Text = "عدد نتائج البحث = " + count.ToString();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool re = School.Service.Bus.AddRegion(Convert.ToInt32(ddCamp.SelectedValue.ToString()), txtcode.Text, txtName.Text);

            if (re == true)
            {
                lbltest.Text = "تمت الاضافة بنجاح";
                ddlBind(ddCamp, School.Service.Bus.GetAllCampuses().ToList());
                txtcode.Text = "";
                txtName.Text = "";
            }
            else
            {
                lbltest.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltest.Visible = true;
        }
        protected void gvRegion_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvRegion.EditIndex = e.NewEditIndex;
            gvRegionBind();
        }
        protected void gvRegion_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvRegion.EditIndex = -1;
            gvRegionBind();
        }
        protected void gvRegion_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvRegion.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Service.Bus.DeleteDistrict(Id);
            if (re == true)
            {
                lbltestsearch.Text = "تم الحذف بنجاح";
                gvRegionBind();
            }
            else
            {
                lbltestsearch.Text = "لم تتم عملية الحذف..ربما تكون المنطقة التي تحاول حذفها مستخدمة";
            }
            lbltestsearch.Visible = true;
        }
        protected void gvRegion_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gvRegion.DataKeys[e.RowIndex].Value.ToString());
            TextBox Name = (TextBox)gvRegion.Rows[e.RowIndex].FindControl("txtN");
            TextBox Code = (TextBox)gvRegion.Rows[e.RowIndex].FindControl("txtC");
            bool re = School.Service.Bus.UpdateDistrict(Id, Code.Text, Name.Text);
            if (re == true)
            {
                lbltestsearch.Text = "تم التحديث بنجاح";
                gvRegion.EditIndex = -1;
                gvRegionBind();
            }
            else
            {
                lbltestsearch.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltestsearch.Visible = true;
        }
        protected void gvRegion_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRegion.PageIndex = e.NewPageIndex;
            gvRegionBind();
        }
        #endregion
        protected void btnexit_Click(object sender, EventArgs e)
        {
            plNew.Visible = false;
            Plsearch.Visible = true;
            ddlBind(ddCampse, School.Service.Bus.GetAllCampuses().ToList());
            gvRegionBind();
        }
        protected void btnadd_Click(object sender, EventArgs e)
        {

            plNew.Visible = true;
            Plsearch.Visible = false;
        }
        protected void btnsearch_Click(object sender, EventArgs e)
        {
            gvRegionBind();
        }

        protected void gvRegion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }
    }
}